package io.openmessaging.demo;

import com.markhsiu.store.disk.MessageUtil;

import io.openmessaging.BytesMessage;
import io.openmessaging.Message;

public class DefaultBytesMessage implements BytesMessage {

	private DefaultKeyValue  headers;
	private DefaultKeyValue properties;
	private String topic;
	private String ID;
	 
	private byte[] body;

	public DefaultBytesMessage(byte[] body) {
		this.body = body;
		headers = new DefaultKeyValue();
		properties = new DefaultKeyValue();
	}
	
	public DefaultBytesMessage(byte[] body, boolean extension) {
		this.body = body;
		headers = new DefaultKeyValue(extension);
		properties = new DefaultKeyValue(extension);
	}

	public DefaultBytesMessage() {
		headers = new DefaultKeyValue();
		properties = new DefaultKeyValue();
	}
	
	public DefaultBytesMessage(boolean extension) {
		headers = new DefaultKeyValue(extension);
		properties = new DefaultKeyValue(extension);
	}
	
	public void init(String topic, byte[] body, String type){
		 this.topic=topic;
		 this. body = body;
	}
	

	@Override
	public byte[] getBody() {
		return body;
	}

	@Override
	public BytesMessage setBody(byte[] body) {
		this.body = body;
		return this;
	}

	@Override
	public DefaultKeyValue headers() {
		return headers;
	}

	@Override
	public DefaultKeyValue properties() {
		return properties;
	}

	@Override
	public Message putHeaders(String key, int value) {
		headers.put(key, value);
		return this;
	}

	@Override
	public Message putHeaders(String key, long value) {
		headers.put(key,value);
		return this;
	}

	@Override
	public Message putHeaders(String key, double value) {
		headers.put(key, value);
		return this;
	}

	@Override
	public Message putHeaders(String key, String value) {
		if(key.equals(MessageUtil.ID)){
			ID = value;
			return this;
		}
		headers.put(key, value);
		return this;
	}

	@Override
	public Message putProperties(String key, int value) {
		properties.put(key,  value);
		return this;
	}

	@Override
	public Message putProperties(String key, long value) {
		properties.put(key, value);
		return this;
	}

	@Override
	public Message putProperties(String key, double value) {
		properties.put(key, value);
		return this;
	}

	@Override
	public Message putProperties(String key, String value) {
		properties.put(key, value);
		return this;
	}

	
	public Message putAllHeaders(DefaultKeyValue keyValue) {
		if(keyValue != null){
				headers = keyValue;
		}
		return this;
	}


	public Message putAllProperties(DefaultKeyValue keyValue) {
		if(keyValue != null){		 
				properties = keyValue;
		}
		return this;
	}
	
	public int len(){
		return  headers.size() + properties.size() +body.length + ID.length() + 4 * 2 ;
	}
	
	public void setTopic(String topic) {
		this.topic = topic;
	}

	public void clear(){
		headers.clear();
		properties.clear();
	} 
	
	
	
	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	public void reset(){
		headers.reset();
		properties.reset();
	} 
	
	public String getTopic(){
		return topic;
	}
	
	public String propertiesToString(){
		return properties.toString();
	}
	
	public String headersToString(){
		return headers.toString();
	}
}
